#!/usr/bin/perl -wT

print "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n";
nest(0, 5, 5, 'span');
print "</div>\n";

:nth-child(-1n+6) :nth-child(-2) :nth-child(-2n-2)

sub nest {
    my($indent, $remaining, $count, $element, @position) = @_;
    ++$indent;
    --$remaining;
    for (my $index = 0; $index < $count; ++$index) {
        if ($remaining > 1) {
            print((' ' x $indent) . "<$element>\n");
            nest($indent, $remaining, $count, $element, @position, $index);
            print((' ' x $indent) . "</$element>\n");
        } elsif ($remaining) {
            print((' ' x $indent) . "<$element> ");
            nest($indent, $remaining, $count, $element, @position, $index);
            print "</$element>\n";
        } else {
            my $class = tryToMatch(\@position, [nthChild(-1, 6), nthChild(0, -2), nthChild(-2, -2)]) ? 'match' : 'nomatch';
            print "<$element class="$class">Test</$element> ";
        }
    }
}

sub nthChild {
    my($position, $counts) = @_;
    if 
}